import sys
from PyQt5.QtWidgets import (
    QApplication, QWidget, QVBoxLayout, QPushButton, 
    QStackedWidget, QLabel, QHBoxLayout
)

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.stacked_widget = QStackedWidget()
        self.init_ui()
        
    def init_ui(self):
        # 创建子页面
        self.page1 = QWidget()
        self.page2 = QWidget()
        self._setup_pages()

        # 切换按钮
        btn_layout = QHBoxLayout()
        btn_page1 = QPushButton("页面1", clicked=lambda: self.switch_page(0))
        btn_page2 = QPushButton("页面2", clicked=lambda: self.switch_page(1))
        btn_layout.addWidget(btn_page1)
        btn_layout.addWidget(btn_page2)

        # 主布局
        main_layout = QVBoxLayout()
        main_layout.addLayout(btn_layout)
        main_layout.addWidget(self.stacked_widget)
        self.setLayout(main_layout)

    def _setup_pages(self):
        # 页面1：文本内容
        page1_layout = QVBoxLayout()
        page1_layout.addWidget(QLabel("功能界面1"))
        self.page1.setLayout(page1_layout)

        # 页面2：按钮操作
        page2_layout = QVBoxLayout()
        page2_layout.addWidget(QPushButton("点击触发操作"))
        self.page2.setLayout(page2_layout)

        # 添加到堆叠容器
        self.stacked_widget.addWidget(self.page1)
        self.stacked_widget.addWidget(self.page2)

    def switch_page(self, index):
        self.stacked_widget.setCurrentIndex(index)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.setWindowTitle("QStackedWidget 示例")
    window.resize(400, 300)
    window.show()
    sys.exit(app.exec())